package com.atguigu.daijia.order.service.impl;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.model.entity.order.OrderMonitor;
import com.atguigu.daijia.model.entity.order.OrderMonitorRecord;
import com.atguigu.daijia.order.mapper.OrderMonitorMapper;
import com.atguigu.daijia.order.repository.OrderMonitorRecordRepository;
import com.atguigu.daijia.order.service.OrderMonitorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderMonitorServiceImpl extends ServiceImpl<OrderMonitorMapper, OrderMonitor> implements OrderMonitorService {


    @Autowired
    private OrderMonitorRecordRepository recordRepository;
    @Autowired
    private OrderMonitorMapper orderMonitorMapper;

    //保存订单录音文本数据到MongoDB
    @Override
    public Boolean saveOrderMonitorRecord(OrderMonitorRecord orderMonitorRecord) {
        recordRepository.save(orderMonitorRecord);
        return true;
    }

    //保存订单监控信息到数据库，返回Id
    @Override
    public Long saveOrderMonitor(OrderMonitor orderMonitor) {
        orderMonitorMapper.insert(orderMonitor);
        return orderMonitor.getId();
    }

    //从数据库根据订单id获取订单监控信息
    @Override
    public OrderMonitor getOrderMonitor(Long orderId) {
        LambdaQueryWrapper<OrderMonitor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderMonitor::getOrderId, orderId);
        OrderMonitor orderMonitor = orderMonitorMapper.selectOne(wrapper);
        return orderMonitor;
    }

    //更新订单监控信息
    @Override
    public Boolean updateOrderMonitor(OrderMonitor orderMonitor) {
        int rows = orderMonitorMapper.updateById(orderMonitor);
        if (rows <= 0) {
            throw new GuiguException(ResultCodeEnum.UPDATE_ERROR);
        }
        return true;
    }
}
